import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class WebScraperToMySQL {
    // MySQL connection details
    private static final String DB_URL = "jdbc:mysql://localhost:3306/web_scraper?useSSL=false&&serverTimezone=UTC";
    private static final String DB_USER = "root"; // Replace with your MySQL username
    private static final String DB_PASSWORD = "root"; // Replace with your MySQL password

    public static void main(String[] args) {

        String url = "http://localhost:8080";


        StringBuilder content = new StringBuilder();

        // Step 1: Scrape the webpage
//            Document doc = Jsoup.connect(url)
//                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
//                    .timeout(10000)
//                    .get();


//            Document doc =  WebCrawler.crawlPage(url);
//
//            // Select main content (adjust selector based on page structure)
//            Elements elements = doc.select("main, article, div.content, div[class*=content], p, h1, h2, h3");
//
//            if (elements.isEmpty()) {
//               log.info("No main content found. Check the page structure or selectors.");
//                return;
//            }
//
//            // Extract and aggregate text content
//            for (Element element : elements) {
//                if (!element.tagName().equals("script") && !element.hasClass("footer") && !element.hasClass("header")) {
//                    String text = element.text().trim();
//                    if (!text.isEmpty()) {
//                        content.append(text).append("\n");
//                    }
//                }
//            }
//
//            if (content.length() == 0) {
//               log.info("No valid content extracted.");
//                return;
//            }
//
//            // Step 2: Save to MySQL
//            saveToDatabase(url, content.toString());
//           log.info("Content successfully scraped and saved to MySQL.");

    }

    private static void saveToDatabase(String url, String content) {
        String sql = "INSERT INTO scraped_content (url, content) VALUES (?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // Set parameters
            pstmt.setString(1, url);
            pstmt.setString(2, content);

            // Execute the insert
            pstmt.executeUpdate();

        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
        }
    }
}